<?php

class User extends CActiveRecord
{
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return 'users';
    }

    public function attributeLabels()
    {
        return array(
            'id'             => Yii::t('main', 'ID'),
            'first_name'     => Yii::t('main', 'First Name'),
            'last_name'      => Yii::t('main', 'Last Name'),
            'email'          => Yii::t('main', 'Email'),
            'phone'          => Yii::t('main', 'Phone'),
            'social_network' => Yii::t('main', 'Social network'),
            'create_time'    => Yii::t('main', 'Create time'),
            'update_time'    => Yii::t('main', 'Update time'),
        );
    }

    public function behaviors()
    {
        return array(
            'CTimestampBehavior' => array(
                'class' => 'zii.behaviors.CTimestampBehavior',
            ),
            'UserActivity'       => array(
                'class' => 'UserActivityLogBehaviour',
            ),
        );
    }

    public function rules()
    {
        return array(
            array('id, first_name, last_name, email, phone, social_network, create_time, update_time', 'safe'),
        );
    }

    public function relations()
    {
        return array(
            'rating'  => array(self::HAS_ONE, 'Rating', 'user_id'),
        );
    }

    public function scopes()
    {
        return array();
    }

    public function search()
    {
        $criteria = new CDbCriteria;

        $criteria->compare('first_name', $this->first_name, true);
        $criteria->compare('last_name', $this->last_name, true);
        $criteria->compare('create_time', $this->create_time, true);
        $criteria->compare('update_time', $this->update_time, true);

        $pagination = array('pageSize' => 20);
        $sort       = array('defaultOrder' => 't.create_time DESC');

        return new CActiveDataProvider(get_class($this), compact('criteria', 'sort', 'pagination'));
    }

    public function forExport()
    {
        $rawData = Yii::app()->db->createCommand('SELECT * FROM users')->queryAll();

        $pagination = array('pageSize' => 20);
        $sort       = array('defaultOrder' => 't.create_time DESC');

        return new CArrayDataProvider(get_class($this), compact('rawData', 'sort', 'pagination'));
    }

}
